package com.easyworkflow.web.spring;

import com.business.domain.T_task_instance;
import com.business.domain.T_task_node;
import com.business.domain.T_task_node_instance;
import com.business.service.T_taskService;
import com.business.service.T_task_instanceService;
import com.business.service.T_task_nodeService;
import com.business.service.T_task_node_instanceService;
import com.common.db.JsonVO;
import com.common.db.Pg;
import com.common.utils.DateUtil;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@Controller
@RequestMapping("/workflow")
public class MyWorkFlowController extends WorkFlowController {
    private static Logger log = Logger.getLogger(WorkFlowController.class.getName());

    /**
     * 1、启动工作流，返回成功或者失败信息json格式结果如：result:[success/error]
     * 业务申请页面点击“发起申请”按钮时，带参数流程id:taskid、业务申请id：appid；去后台查询该流程对应节点数据，
     * 找到第一个节点数据，插入流程实例表、流程节点实例表，生成第一岗待审批数据，即待办任务；
     *
     * @param taskid
     * @param appid
     * @param request
     * @return JsonVO
     */
    @RequestMapping("/startTask")
    @ResponseBody
    public JsonVO startTask(int taskid, String biztype, int appid, HttpServletRequest request) {
        return startMyTask( taskid, biztype, appid, request);
    }

    /**
     * 2、审核通过，返回成功或者失败信息json格式结果如：result:[success/error]
     * 点击业务审核页面“审核通过”按钮时，提交参数taskNodeInstanceid流程节点实例id；
     * 去查询对应流程节点实例数据，并更新该条待办任务为已办理成功，
     * 判断是否是最有一岗，如果是最后一岗，则直接更新流程实例数据为已完成；
     * 如果不是最后一岗，则查询下一岗数据，插入流程节点实例表，即下一岗待办任务；
     *
     * @param taskNodeInstanceid
     * @param request
     * @return JsonVO
     */
    @RequestMapping("/doAgree")
    @ResponseBody
    public JsonVO doAgree(int taskNodeInstanceid, String note, HttpServletRequest request) {
        return doMyAgree(taskNodeInstanceid,note, request);
    }

    /**
     * 4、修改T_task_instance信息并且返回成功或者失败信息json格式结果如：result:[success/error]
     *
     * @param taskNodeInstanceid
     * @param request
     * @return JsonVO
     */
    @RequestMapping("/doBack")
    @ResponseBody
    public JsonVO doBack(int taskNodeInstanceid, String note, HttpServletRequest request) {
        return doMyBack(taskNodeInstanceid, note, request);
    }

    /**
     * 5、修改T_task_instance信息并且返回成功或者失败信息json格式结果如：result:[success/error]
     *
     * @param t_task_instance
     * @param request
     * @return JsonVO
     */
    @RequestMapping("/doReStart")
    @ResponseBody
    public JsonVO doReStart(T_task_instance t_task_instance, javax.servlet.http.HttpServletRequest request) {
        return doMyReStart(t_task_instance, request);
    }

    /**
     * 强制结束，不通过；
     * 6、修改T_task_instance信息并且返回成功或者失败信息json格式结果如：result:[success/error]
     * @param taskNodeInstanceid
     * @param note
     * @param request
     * @return JsonVO
     */
    @RequestMapping("/doFinish")
    @ResponseBody
    public JsonVO doFinish(int taskNodeInstanceid, String note, javax.servlet.http.HttpServletRequest request) {
        return doMyFinish(taskNodeInstanceid,note, request);
    }

    @RequestMapping(value = "/myTask")
    public String myTask(javax.servlet.http.HttpServletRequest request, HttpServletResponse response) {
        log.debug("------跳转到后台登录页面t_task_node_instance/mytask.jsp------");
        return "business/t_task_node_instance/myTask";
    }

    @RequestMapping(value = "/myTaskHis")
    public String myTaskHis(javax.servlet.http.HttpServletRequest request, HttpServletResponse response) {
        log.debug("------跳转到后台登录页面t_task_node_instance/mytask.jsp------");
        return "business/t_task_node_instance/myTaskHis";
    }

    /**
     * 获取 待办任务列表
     * @param request
     * @return Pg，json格式
     */
    @RequestMapping("/getTaskNodeInstanceList")
    @ResponseBody
    public Pg getTaskNodeInstanceList(javax.servlet.http.HttpServletRequest request) {
        return getMyList(request);
    }


    /**
     * 获取 流程审核所有意见记录；
     * @param request
     *  最终 根据instanceid 查询该任务的所有审核意见记录；
     * @return Pg，json格式
     */
    @RequestMapping("/getTaskNodeInstanceNoteList")
    @ResponseBody
    public Pg getTaskNodeInstanceNoteList(javax.servlet.http.HttpServletRequest request) {
        return getMyNoteList(request);
    }



}